﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BLL;

public partial class PayTypeManager : System.Web.UI.Page
{
    FinaceTypeBll bll = new FinaceTypeBll();  

    protected int TreeSelectTypeID
    {
        get
        {
            return int.Parse(hdnSelectValue.Value);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.ProcessAjax();

        if (!Page.IsPostBack)
        {
            this.btnDel.Attributes["style"] = "display:none";
            Response.Expires = -1;
        }
    }

    private void ProcessAjax()
    {
        if (Request["AjaxType"] == null)
        {
            return;
        }

        this.GetTypeInfoJson(int.Parse(Request["PayTypeID"]));
    }

    private void GetTypeInfoJson(int typeID)
    {
        string json = "{TypeName:'@@TypeName',Remark:'@@Remark'}";

        FinaceType finaceType = null;
        if (typeID != -1)
        {
            finaceType = bll.SelectSingleFinaceType(typeID);
        }
        else
        {

            finaceType = new FinaceType() { TypeName = "所有分类", Remark = "" };
        }

        json = json.Replace("@@TypeName", finaceType.TypeName);
        json = json.Replace("@@Remark", finaceType.Remark);
        Response.Write(json);
        Response.End();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        FinaceType model = new FinaceType();
        model.MemberID = HttpMemberInfo.MemerID;
        model.TypeName = this.txtPayType.Text;
        model.Remark = this.txtRemark.Text;
        model.ParentTypeID = this.TreeSelectTypeID;

        bll.AddFinaceType(model);

        this.txtPayType.Text = String.Empty;
        this.txtRemark.Text = String.Empty;
    }

    protected void btnDel_Click(object sender, EventArgs e)
    {
        int[] notAllowDeleteType = new int[] { -1, 2, 3 };

        if ( Array.IndexOf(notAllowDeleteType, this.TreeSelectTypeID) != -1)
        {
            Library.WebCommon.Alert(this, "该类型不能被删除");
            return;
        }
        
        if (!bll.IsCanDel(this.TreeSelectTypeID))
        {
            Library.WebCommon.Alert(this, "该类型已经存在财务记录,不能被删除");
            return;
        }

        bll.DelFinaceType(this.TreeSelectTypeID);



        this.txtEditFinaceType.Text = String.Empty;
        this.txtEditRemark.Text = String.Empty;

        this.hdnSelectValue.Value = "2";//2表示支出
        this.txtEditFinaceType.Text = "支出";
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        FinaceType model = new FinaceType();
        model.TypeID = this.TreeSelectTypeID;
        model.TypeName = this.txtEditFinaceType.Text;
        model.Remark = this.txtEditRemark.Text;

        bll.UpdateFinaceType(model);

        Library.WebCommon.Alert(this, "保存成功");
    }
}
